/*
 * vws_http_request.cpp
 *
 *  Created on: Sep 20, 2013
 *      Author: vscen
 */

#include "vws_http_request.h"

namespace vws {
void vws_http_request::begin_analyize(const char* request,
		const char** response, std::string& errors) {
	std::list<std::string> proto;
	std::string method;
	std::string host;
	if (!vws_http_request::valid_line_count(request, proto)) {
		char resp_head[PATH_MAX] = { 0 };
		vws_http_error::get_status_buffer(400, resp_head, PATH_MAX);
		errors.append(resp_head);
		vws_http_error::get_error_page(400, resp_head, PATH_MAX);
		errors.append(resp_head);
		return;
	} else {
		std::list<std::string>::iterator it;
		for (it = proto.begin(); it != proto.end(); it++) {
			std::string request = *it;
			if (vws_http_request::check_basic_method(request.c_str(), method)) {
				proto.remove(request);
			} else if (vws_http_request::check_basic_host(request.c_str(), host)) {
				proto.remove(request);
			} else {
				continue;
			}
		}
		if ((method.length() == 0) || (host.length() == 0)) {
			char resp_head[PATH_MAX] = { 0 };
			vws_http_error::get_status_buffer(400, resp_head, PATH_MAX);
			errors.append(resp_head);
			vws_http_error::get_error_page(400, resp_head, PATH_MAX);
			errors.append(resp_head);
			return;
		}
	}

}
void vws_http_request::end_analyize() {

}
bool vws_http_request::valid_line_count(const char* request, std::list<
		std::string>& proto) {
	std::string line = "";
	for (int i = 0; request[i] != 0; i++) {
		line.clear();
		while (!vws_http_request::is_enter_and_newline(&request[i])) {
			line.append(&request[i++]);
		}
		if (line.length() > 0) {
			proto.push_back(line);
		}
	}
	return (proto.size() >= 2);
}

bool vws_http_request::check_basic_method(const char* request,
		std::string& method) {
	if (!strncasecmp(request, "get", 3)) {
		vws_http_request::skip_step_pos(&request, 3);
		vws_http_request_const::request_method = vws_http_request_const::GET;
	} else if (!strncasecmp(request, "post", 4)) {
		vws_http_request::skip_step_pos(&request, 4);
		vws_http_request_const::request_method = vws_http_request_const::POST;
	} else {
		return false;
	}
	while (!vws_http_request::skip_space(&request)) {
		char c = *request;
		method.push_back(c);
	}
	return true;
}

bool vws_http_request::check_basic_host(const char* request, std::string& host) {
	if (!strncasecmp(request, "host:", 5)) {
		vws_http_request::skip_step_pos(&request, 5);
		if (vws_http_request::skip_space(&request)) {
			host.append(request);
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

bool vws_http_request::is_enter_and_newline(const char* line) {
	return (line[0] == '\r' && line[1] == '\n');
}

bool vws_http_request::skip_space(const char** line) {
	const char* request = *line;
	while (*request == ' ' && *request != 0) {
		++request;
	}
	*line = request;
	return (*request != 0);
}

bool vws_http_request::skip_step_pos(const char** line, int count) {
	const char* request = *line;
	for (int i = 0; i < count; i++) {
		request++;
	}
	*line = request;
	return true;
}

}
